﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNhaThuoc.DTO;
using QLNhaThuoc.BL;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc
{
    public partial class Form_DangNhap : Form
    {
        public Form_DangNhap()
        {
            InitializeComponent();
        }

        NhanVienControl nhanVienCtrl;

        private void Form_DangNhap_Load(object sender, EventArgs e)
        {
            nhanVienCtrl = new NhanVienControl();
        }

        private void btDangNhap_Click(object sender, EventArgs e)
        {
            DangNhap(); 
        }

        private void DangNhap()
        {
            if (KiemTraDuLieuNhap() == true)
            {
                NhanVien nv = nhanVienCtrl.LayNhanVienTheoUsername(tbUsername.Text);
                if (nv == null)
                {
                    Utils.ShowError("Username không tồn tại", "Thông báo");
                }
                else
                {
                    if (nv.Password != tbPassword.Text)
                        Utils.ShowError("Sai mật khẩu", "Thông báo");
                    else
                    {
                        Form_QLNT frm = (Form_QLNT)this.MdiParent;
                        nv.CapQuyenSuDung(frm);

                        this.Close();
                    }
                }
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool KiemTraDuLieuNhap()
        {
            if (tbUsername.Text == "")
            {
                Utils.ShowError("Bạn chưa nhập username", "Thông báo");
                return false;
            }

            if (tbPassword.Text == "")
            {
                Utils.ShowError("Bạn chưa nhập password", "Thông báo");
                return false;
            }

            return true;
        }

        private void Form_DangNhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void Form_DangNhap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                DangNhap();
        }

        private void tbUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                DangNhap();
        }

        private void tbPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                DangNhap();
        }


    }
}
